package com.eastfair.home.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.home.dto.ReportSpaceAuditDTO;
import com.eastfair.home.dto.ReportSpaceAuditPageQuery;
import com.eastfair.home.dto.ReportSpaceAuditSaveDTO;
import com.eastfair.home.dto.ReportSpaceAuditUpdateDTO;
import com.eastfair.home.entity.ReportSpaceAudit;
import com.eastfair.home.service.ReportSpaceAuditService;
import com.eastfair.home.vo.ReportSpaceAuditVO;
import com.eastfair.venueservice.dto.ServiceItemDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 报馆审核记录
 * </p>
 *
 * @author dq
 * @date 2022-08-08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/reportSpaceAudit")
@Api(value = "ReportSpaceAudit", tags = "报馆审核记录")
@PreAuth(replace = "home:reportSpaceAudit:")
public class ReportSpaceAuditController extends SuperController<ReportSpaceAuditService, Long, ReportSpaceAudit, ReportSpaceAuditPageQuery, ReportSpaceAuditSaveDTO, ReportSpaceAuditUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private ReportSpaceAuditService reportSpaceAuditService;

    @Override
    public void handlerResult(IPage<ReportSpaceAudit> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<ReportSpaceAudit> reportSpaceAuditList = list
                .stream()
                .map((map) -> ReportSpaceAudit.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(reportSpaceAuditList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ReportSpaceAuditPageQuery> params) {
        ReportSpaceAuditPageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 分页查询报馆记录审核列表.
     *
     * @param pageParams the page params
     * @return the r
     */
    @ApiOperation("分页查询报馆记录审核列表")
    @PostMapping("/query/reportSpace")
    public R<IPage<ReportSpaceAuditVO>> queryReportSpace(@RequestBody PageParams<ReportSpaceAuditDTO> pageParams) {
        log.info("queryReportSpace - 分页查询报馆记录审核列表, pageParams={}", pageParams);
        return R.successDef(reportSpaceAuditService.listReportSpaceAuditVOs(pageParams));
    }

    /**
     * ID获取报馆审核VO.
     *
     * @param id the id
     * @return the by id
     */
    @ApiOperation("ID获取报馆审核VO")
    @GetMapping("/get/{id}")
    public R<ReportSpaceAuditVO> getById(@PathVariable("id") Long id) {
        log.info("getById - ID获取报馆审核VO, id={}", id);
        return R.successDef(reportSpaceAuditService.getVOById(id));
    }

    /**
     * 提交报馆审核.
     *
     * @param reportSpaceAuditDTO the report space audit dto
     * @return the r
     */
    @ApiOperation("提交报馆审核")
    @PostMapping("/submit")
    public R<Boolean> submitReportSpaceAudit(@RequestBody ReportSpaceAuditDTO reportSpaceAuditDTO) {
        log.info("submitReportSpaceAudit - 提交报馆审核, reportSpaceAuditDTO={}", reportSpaceAuditDTO);
        try {
            return R.successDef(reportSpaceAuditService.submitReportSpaceAudit(reportSpaceAuditDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 修改审核结果.
     *
     * @param reportSpaceAuditDTO the report space audit dto
     * @return the r
     */
    @ApiOperation("修改审核结果")
    @PostMapping("/update/reportSpaceStatus")
    public R<Boolean> updateReportSpaceStatus(@RequestBody ReportSpaceAuditDTO reportSpaceAuditDTO) {
        log.info("updateReportSpaceStatus - 修改审核结果, reportSpaceAuditDTO={}", reportSpaceAuditDTO);
        return R.successDef(reportSpaceAuditService.updateReportSpaceStatus(reportSpaceAuditDTO));
    }

    /**
     * 取消报馆申请.
     *
     * @param reportSpaceId the report space id
     * @return the r
     */
    @ApiOperation("取消报馆申请")
    @PostMapping("/cancel/submit/{reportSpaceId}")
    public R<Boolean> cancelSubmit(@PathVariable("reportSpaceId") Long reportSpaceId) {
        log.info("cancelSubmit - 取消报馆申请, reportSpaceId={}", reportSpaceId);
        try {
            return R.successDef(reportSpaceAuditService.cancelReportSpaceSubmit(reportSpaceId));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 发送配置收款商户的站内信.
     *
     * @param orderType           the order type
     * @param reportSpaceAuditDTO the report space audit dto
     * @return the r
     */
    @PostMapping("/send/config/payee/innerMsg")
    public R<Boolean> sendConfigPayeeInnerMsg(@RequestParam("orderType") String orderType, @RequestBody ReportSpaceAuditDTO reportSpaceAuditDTO) {
        log.info("sendConfigPayeeInnerMsg - 发送配置收款商户的站内信, reportSpaceAuditDTO={}", reportSpaceAuditDTO);
        try {
            return R.successDef(reportSpaceAuditService.sendConfigPayeeInnerMsg(reportSpaceAuditDTO, orderType));
        } catch (BizException e) {
            return R.fail(e);
        }
    }


    /**
     * 校验是否可以编辑管理费服务或规格
     * @param serviceItemDTO
     * @return
     */
    @PostMapping("/checkServiceCategory")
    public R<Boolean> checkServiceCategory(@RequestBody ServiceItemDTO serviceItemDTO) {
        log.info("checkServiceCategory - 发送配置收款商户的站内信, serviceItemDTO={}", serviceItemDTO);
        try {
            return R.successDef(reportSpaceAuditService.checkServiceCategory(serviceItemDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }
}
